package com.csun.dsms.utils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

public class WorkingDaysCalculator {

    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2025, 5, 1);
        LocalDate endDate = LocalDate.of(2025, 5, 12);
        int workDays = calculateWorkDays(startDate, endDate);
        System.out.println("工作天数：" + workDays);


    }


    // 定义法定节假日列表
    private static final List<LocalDate> HOLIDAYS = new ArrayList<LocalDate>() {{
        add(LocalDate.of(2025, 1, 1)); // 元旦
        add(LocalDate.of(2025, 5, 1)); // 劳动节
        add(LocalDate.of(2025, 5, 2)); // 劳动节
        add(LocalDate.of(2025, 5, 3)); // 劳动节
        add(LocalDate.of(2025, 5, 4)); // 劳动节
        add(LocalDate.of(2025, 5, 5)); // 劳动节
        add(LocalDate.of(2025, 6, 1)); // 儿童节
        add(LocalDate.of(2025, 9, 10)); // 教师节
        add(LocalDate.of(2025, 10, 1)); // 国庆节
        add(LocalDate.of(2025, 10, 2));
        add(LocalDate.of(2025, 10, 3));
        add(LocalDate.of(2025, 10, 4));
        add(LocalDate.of(2025, 10, 5));
        add(LocalDate.of(2025, 10, 6));
        add(LocalDate.of(2025, 10, 7));
    }};

    // 计算两个日期之间的工作时间（天数）
    public static int calculateWorkDays(LocalDate startDate, LocalDate endDate) {
        int workDays = 0;
        LocalDate date = startDate;

        while (date.isBefore(endDate)) {
            if (!HOLIDAYS.contains(date) && date.getDayOfWeek() != DayOfWeek.SATURDAY && date.getDayOfWeek() != DayOfWeek.SUNDAY) {
                workDays++;
            }
            date = date.plusDays(1);
        }

        return workDays;
    }

    // 计算两个日期之间的工作时间（小时数），每天计算 8 小时
    public static long calculateWorkHours(LocalDate startDate, LocalDate endDate) {
        int workDays = calculateWorkDays(startDate, endDate);
        long workHours = workDays * 8L;

        // 计算开始日期当天的工作时间
        if (!HOLIDAYS.contains(startDate) && startDate.getDayOfWeek() != DayOfWeek.SATURDAY && startDate.getDayOfWeek() != DayOfWeek.SUNDAY) {
            long remainingHours = ChronoUnit.HOURS.between(startDate.atStartOfDay(), endDate.atStartOfDay());
            workHours -= Math.max(0, 8L - remainingHours);
        }

        // 计算结束日期当天的工作时间
        if (!HOLIDAYS.contains(endDate) && endDate.getDayOfWeek() != DayOfWeek.SATURDAY && endDate.getDayOfWeek() != DayOfWeek.SUNDAY) {
            long remainingHours = ChronoUnit.HOURS.between(endDate.atStartOfDay(), startDate.atStartOfDay());
            workHours -= Math.max(0, 8L - remainingHours);
        }

        return workHours;
    }


}
